﻿using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using Microsoft.ApplicationBlocks.Data;


namespace AnyOffice.general.rms.roll_manage
{
    public partial class query : ModulePage
    {
        protected string strTemp;

        public string GetCodeName(string codeId, string codeType)
        {
            if (codeType == "Secret")
            {
                return new RmsCodeDao().GetCodeName(base.Conn, codeId, RmsCodeDao.CodeType.Secret);
            }
            if (codeType == "CertificateKind")
            {
                return new RmsCodeDao().GetCodeName(base.Conn, codeId, RmsCodeDao.CodeType.CertificateKind);
            }
            return string.Empty;
        }
        private void BindDeptTree(ref DropDownList ddlParentDept, int DeptId, string strTemp)
        {
            DataTable table = new DepartmentDao().SelectChildAll(base.Conn, DeptId);
            if (table.Rows.Count > 0)
            {
                strTemp = strTemp + "|　";
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    ListItem item = new ListItem(strTemp + "|─" + table.Rows[i]["DeptName"].ToString(), table.Rows[i]["DeptId"].ToString());
                    ddlParentDept.Items.Add(item);
                    this.BindDeptTree(ref ddlParentDept, int.Parse(table.Rows[i]["DeptId"].ToString()), strTemp);
                }
            }
        } 

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                DataTable table = new RmsRoomDao().SelectAll(base.Conn);
                this.ddlRoomId.DataSource = table;
                this.ddlRoomId.DataBind();
                this.ddlRoomId.Items.Insert(0, new ListItem("", ""));
                ListItem item = new ListItem("", "");
                this.ddlDeptId.Items.Add(item);
                DataTable table2 = new DepartmentDao().SelectParentAll(base.Conn);
                if (table2.Rows.Count > 0)
                {
                    for (int i = 0; i < table2.Rows.Count; i++)
                    {
                        item = new ListItem("|─" + table2.Rows[i]["DeptName"].ToString(), table2.Rows[i]["DeptId"].ToString());
                        this.ddlDeptId.Items.Add(item);
                        this.strTemp = "";
                        this.BindDeptTree(ref this.ddlDeptId, int.Parse(table2.Rows[i]["DeptId"].ToString()), this.strTemp);
                    }
                }
                this.ddlSecret.DataSource = new RmsCodeDao().SelectAll(base.Conn, RmsCodeDao.CodeType.Secret);
                this.ddlSecret.DataBind();
                this.ddlSecret.Items.Insert(0, new ListItem("", ""));
                this.ddlCertificateKind.DataSource = new RmsCodeDao().SelectAll(base.Conn, RmsCodeDao.CodeType.CertificateKind);
                this.ddlCertificateKind.DataBind();
                this.ddlCertificateKind.Items.Insert(0, new ListItem("", ""));
            }
            if (this.Page.IsPostBack)
            {
                this.ViewState["CurrentPage"] = 1;
                this.litNavTitle.Text = "案卷查询结果";
                this.pnlMain.Visible = false;
                SqlCommand command = new SqlCommand();
                command.Connection = base.Conn;
                ArrayList list = new ArrayList();
                string commandText = "SELECT [Rms_Roll].[RollId],[Rms_Roll].[RollCode],[Rms_Roll].[RollName],[Rms_Roll].[CategoryNo],[Rms_Roll].[CertificateKind],[Rms_Roll].[Secret],[Rms_Roll].[Status],[Rms_Room].[RoomName]\tFROM [dbo].[Rms_Roll],[dbo].[Rms_Room] WHERE [Rms_Roll].[RoomId] = [Rms_Room].[RoomId] ";
                if (this.txtRollCode.Text != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[RollCode] LIKE '%' + @RollCode + '%' ";
                    list.Add(new object[] { "@RollCode", this.txtRollCode.Text });
                }
                if (this.txtRollName.Text != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[RollName] LIKE '%' + @RollName + '%' ";
                    list.Add(new object[] { "@RollName", this.txtRollName.Text });
                }
                if (this.ddlRoomId.SelectedValue != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[RoomId] = @RoomId ";
                    list.Add(new object[] { "@RoomId", Convert.ToInt32(this.ddlRoomId.SelectedValue) });
                }
                if (this.txtYears.Text != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[Years] LIKE '%' + @Years + '%' ";
                    list.Add(new object[] { "@Years", this.txtYears.Text });
                }
                if (this.txtBeginDateStart.Value != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[BeginDate] >= @BeginDateStart ";
                    list.Add(new object[] { "@BeginDateStart", Convert.ToDateTime(this.txtBeginDateStart.Value + " 00:00:00") });
                }
                if (this.txtBeginDateEnd.Value != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[BeginDate] <= @BeginDateEnd ";
                    list.Add(new object[] { "@BeginDateEnd", Convert.ToDateTime(this.txtBeginDateEnd.Value + " 23:59:59") });
                }
                if (this.txtEndDateStart.Value != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[EndDate] >= @EndDateStart ";
                    list.Add(new object[] { "@EndDateStart", Convert.ToDateTime(this.txtEndDateStart.Value + " 00:00:00") });
                }
                if (this.txtEndDateEnd.Value != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[EndDate] <= @EndDateEnd ";
                    list.Add(new object[] { "@EndDateEnd", Convert.ToDateTime(this.txtEndDateEnd.Value + " 23:59:59") });
                }
                if (this.ddlDeptId.SelectedValue != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[DeptId] = @DeptId ";
                    list.Add(new object[] { "@DeptId", Convert.ToInt32(this.ddlDeptId.SelectedValue) });
                }
                if (this.txtBZJG.Text != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[BZJG] = @BZJG ";
                    list.Add(new object[] { "@BZJG", this.txtBZJG.Text });
                }
                if (this.txtDeadLine.Text != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[DeadLine] = @DeadLine ";
                    list.Add(new object[] { "@DeadLine", this.txtDeadLine.Text });
                }
                if (this.ddlSecret.SelectedValue != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[Secret] = @Secret ";
                    list.Add(new object[] { "@Secret", this.ddlSecret.SelectedValue });
                }
                if (this.txtCategoryNo.Text != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[CategoryNo] = @CategoryNo ";
                    list.Add(new object[] { "@CategoryNo", this.txtCategoryNo.Text });
                }
                if (this.txtCatalogNo.Text != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[CatalogNo] = @CatalogNo ";
                    list.Add(new object[] { "@CatalogNo", this.txtCatalogNo.Text });
                }
                if (this.txtArchiveNo.Text != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[ArchiveNo] = @ArchiveNo ";
                    list.Add(new object[] { "@ArchiveNo", this.txtArchiveNo.Text });
                }
                if (this.txtBoxNo.Text != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[BoxNo] = @BoxNo ";
                    list.Add(new object[] { "@BoxNo", this.txtBoxNo.Text });
                }
                if (this.txtMicroNo.Text != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[MicroNo] = @MicroNo ";
                    list.Add(new object[] { "@MicroNo", this.txtMicroNo.Text });
                }
                if (this.ddlCertificateKind.SelectedValue != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[CertificateKind] = @CertificateKind ";
                    list.Add(new object[] { "@CertificateKind", this.ddlCertificateKind.SelectedValue });
                }
                if (this.txtCertificateStart.Text != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[CertificateStart] = @CertificateStart ";
                    list.Add(new object[] { "@CertificateStart", this.txtCertificateStart.Text });
                }
                if (this.txtCertificateEnd.Text != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[CertificateEnd] = @CertificateEnd ";
                    list.Add(new object[] { "@CertificateEnd", this.txtCertificateEnd.Text });
                }
                if (this.txtRollPage.Text != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[RollPage] = @RollPage ";
                    list.Add(new object[] { "@RollPage", this.txtRollPage.Text });
                }
                if (this.txtRemark.Text != string.Empty)
                {
                    commandText = commandText + " AND [Rms_Roll].[Remark] LIKE '%' + @Remark + '%' ";
                    list.Add(new object[] { "@Remark", this.txtRemark.Text });
                }
                SqlParameter[] commandParameters = new SqlParameter[list.Count];
                for (int j = 0; j < list.Count; j++)
                {
                    object[] objArray = (object[])list[j];
                    commandParameters[j] = new SqlParameter(objArray[0].ToString(), objArray[1]);
                }
                DataTable table3 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text , commandText, commandParameters).Tables[0];
                if (table3.Rows.Count > 0)
                {
                    this.rptRmsRoll.DataSource = table3;
                    this.rptRmsRoll.DataBind();
                }
                else
                {
                    this.litMessage.Text = base.ShowPrompt("无符合条件的案卷");
                }
            }


        }
    }
}
